<script setup>
import {
  articleGetChannelListService,
  articleDeleteChannelService
} from '@/api/article'
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import DialogContainer from './component/DialogContainer.vue'
import { ElMessageBox, ElMessage } from 'element-plus'

const channelList = ref([])
const loading = ref(false)
const dialogpop = ref(null)

const getChannelList = async () => {
  loading.value = true
  const res = await articleGetChannelListService()
  // console.log(res)
  channelList.value = res.data.data
  loading.value = false
}
getChannelList()

// 添加分类弹窗
const addChannel = () => {
  dialogpop.value.openDialog({})
}
// 编辑分类弹窗
const handleEdit = (row) => {
  // console.log(index, row)
  dialogpop.value.openDialog(row)
}
// 删除分类弹窗
const handleDelete = (index, row) => {
  console.log(index, row)
  ElMessageBox.confirm('确定删除该分类吗？', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      console.log('confirm')
      // 调用删除分类接口
      await articleDeleteChannelService(row.id)
      ElMessage.success('删除成功')
      // 刷新列表
      getChannelList()
    })
    .catch(() => {})
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button @click="addChannel" type="primary">添加分类</el-button>
    </template>
    <el-table v-loading="loading" :data="channelList">
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button
            size="small"
            :icon="Edit"
            circle
            type="primary"
            plain
            @click="handleEdit(scope.row)"
          >
          </el-button>
          <el-button
            size="small"
            type="danger"
            :icon="Delete"
            circle
            plain
            @click="handleDelete(scope.$index, scope.row)"
          >
          </el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据"></el-empty>
      </template>
    </el-table>
    <!--添加和编辑分类的弹窗-->
    <dialog-container
      ref="dialogpop"
      @refresh="getChannelList()"
    ></dialog-container>
  </page-container>
</template>
